﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using 纺织贸易管理系统;

namespace PrintLabel.Tools
{
    public  class RollHelper
    {
       
        /// <summary>
        /// 获取一个缸卷号
        /// </summary>
        /// <param name="juanHaoTable"></param>
        /// <returns></returns>
        public static int CreatGangJuanhao(JuanHaoTable juanHaoTable)
        {
            try
            {
                List<JuanHaoTable> res = new List<JuanHaoTable>();
                //StockTable stock = dbContext.dbcontext.Queryable<StockTable>().First (x => x.ID == stockID);
                res = dbContext.dbcontext.Queryable<JuanHaoTable>().Where(x => x.SampleNum == juanHaoTable.SampleNum
                &&x.yanse==juanHaoTable.yanse
                &&x.kuanhao== juanHaoTable.kuanhao
                &&x.OrderNum == juanHaoTable.OrderNum
                 && x.Houzhengli == juanHaoTable.Houzhengli && x.GangHao == juanHaoTable.GangHao ).OrderByDescending(x => x.PiHao).ToList();
                int pihao = 1;
                if (res.Count == 0)
                {
                    return pihao;
                }
                else
                {
                    pihao = res.First().PiHao + 1;
                    return pihao;
                }

            }
            catch
            {
                return Convert.ToInt32(1);
            }
        }
     
        public static StockTable QueryStockWithRoll(JuanHaoTable roll)
        {
            return  dbContext.dbcontext.Queryable<StockTable>().First(x =>x.houzhengli==roll.Houzhengli
            && x.CKMC == roll.Ckmc 
            && x.BH == roll.SampleNum
            && x.YS == roll.yanse 
            &&x.kuanhao== roll.kuanhao
            &&x.orderNum== roll.OrderNum
            && x.GH == roll.GangHao);
        }
        /// <summary>
        /// 检查这条库存是否有卷
        /// </summary>
        /// <param name="stock"></param>
        /// <returns></returns>
        public static Boolean CheckIsHaveRolls(StockTable stock)
        {
            return dbContext.dbcontext.Queryable<JuanHaoTable>().Any(x =>  x.yanse == stock.YS
            && x.Houzhengli == stock.houzhengli
            && x.kuanhao == stock.kuanhao
             && x.GangHao == stock.GH
             && x.SampleNum == stock.BH 
             && x.OrderNum == stock.orderNum
             && x.Ckmc == stock.CKMC);
        }
      
        public static void IncreaseYijianmishu(StockTable d,JuanHaoTable juan)
        {
            d.biaoqianmishu += juan.biaoqianmishu;
            d.yijianjuanshu++;
            d.NotCheckNum -= juan.biaoqianmishu;
            d.yijianmishu += juan.MiShu;
            //switch (juan.DengJI)
            //{
            //    case "A":
            //        d.GradeA += juan.biaoqianmishu;
            //        break;
            //    case "B":
            //        d.GradeB += juan.biaoqianmishu;
            //        break;
            //    case "C":
            //        d.GradeC += juan.biaoqianmishu;
            //        break;
            //}
        }
        public static void ReduceYijianmishu(StockTable d, JuanHaoTable juan)
        {
            d.biaoqianmishu -= juan.biaoqianmishu;
            d.yijianjuanshu--;
            //d.NotCheckNum += juan.biaoqianmishu;
            d.yijianmishu -= juan.MiShu;
            d.IsCheckDone = "未完成";
            d.Difference=0;
            d.MS -= juan.biaoqianmishu;
            d.JS -= 1;
            //switch (juan.DengJI)
            //{
            //    case "A":
            //        d.GradeA -= juan.biaoqianmishu;
            //        break;
            //    case "B":
            //        d.GradeB -= juan.biaoqianmishu;
            //        break;
            //    case "C":
            //        d.GradeC -= juan.biaoqianmishu;
            //        break;
            //}
        }
        public static void CreateDeleteLog(JuanHaoTable roll)
        {
            var del = new DeleteLog();
            del.date = DateTime.Now;
            del.useID = roll.CheckID;
            var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            string s = serializer.Serialize(roll);
            del.Log = "删除:" + s;
            dbContext.dbcontext.Insertable(del).ExecuteCommand();
        }
        public static string CreateBarcode(string ganghao,string juanhao)
        {
            var danhao = "JH" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString();
            danhao += DateTime.Now.Second.ToString() + ganghao +juanhao;
            return danhao;
        }

        public static void CalculateInventoryQuantity(StockTable stock)
        { 
            stock.MS=stock.biaoqianmishu ;
            stock.JS=stock.yijianjuanshu;
            stock.Difference = stock.NotCheckNum;
            stock.NotCheckNum = 0;
            stock.IsCheckDone = "已完成";
        }
        public static void CreateStock(JuanHaoTable roll)
        {

            var stock = dbContext.dbcontext.Queryable<StockTable>().First(x => x.CKMC == roll.Ckmc
            && x.orderNum == roll.OrderNum 
            && x.BH == roll.SampleNum 
            && x.kuanhao == roll.kuanhao 
            && x.YS == roll.yanse 
            && x.GH == roll.GangHao);
            if (stock != null)
            {
                stock.yijianmishu += roll.MiShu;
                stock.yijianjuanshu++;
                stock.MS += roll.biaoqianmishu;
                stock.JS++;
                dbContext.dbcontext.Updateable(stock).ExecuteCommand();
            }
            else
            {
                stock = new StockTable()
                {
                    MS = roll.biaoqianmishu,
                    JS = 1,
                    BH = roll.SampleNum,
                    PM = roll.SampleName,
                    kuanhao = roll.kuanhao,
                    YS = roll.yanse,
                    GH = roll.GangHao,
                    CustomerPingMing = roll.CustomerPingMing,
                    biaoqianmishu = roll.biaoqianmishu,
                    ContractNum = roll.Hetonghao,
                    GG = roll.guige,
                    Huahao = roll.Huahao,
                    KZ = roll.Kezhong,
                    MF = roll.Menfu,
                    orderNum = roll.OrderNum,
                    CKMC = roll.Ckmc,
                    CustomerColorNum = roll.CustomerColorNum,
                    CustomName = roll.CustomerName,
                    CF = roll.Composition,
                    Density = roll.Density,
                    EnglishName = roll.EnglishName,
                    yijianmishu=roll.MiShu,
                    yijianjuanshu=1,
                    SecondaryEncoding = roll.SecondaryEncoding,
                };
                stock.MapperFrom(roll);
                dbContext.dbcontext.Insertable(stock).ExecuteCommand();
            }
        }
     }
}
